package org.lex.dbq.view.session;

import java.util.Arrays;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.lex.dbq.model.bo.JdbcUrl;
import org.lex.dbq.model.bo.Session;
import org.lex.dbq.model.service.ServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectSessionDialog extends Dialog {
	public static void main(String[] args) {
		ConnectSessionDialog d = new ConnectSessionDialog((Shell) null, null);
		d.open();
	}

	public static JdbcUrl showConnect(Shell parent, String sessionName) {
		ConnectSessionDialog d = new ConnectSessionDialog(parent, sessionName);
		d.open();
		return d.url;
	}

	private String initSessionName = null;
	private Combo session = null;
	private Text user = null;
	private Text password = null;

	private JdbcUrl url = null;

	private Logger log = LoggerFactory.getLogger(ConnectSessionDialog.class);

	public ConnectSessionDialog(Shell parentShell, String sessionName) {
		super(parentShell);
		this.initSessionName = sessionName;
		this.setShellStyle(SWT.MAX | SWT.RESIZE);
	}

	private void selectSession(String sessionName) {
		if (null == sessionName) {
			return;
		}
		Session s = ServiceFactory.getSessionService().getByName(sessionName);
		if (null != s) {
			session.setText(s.getName());
			user.setText(s.getUser());
			password.setText(s.getPassword());
		}
	}

	@Override
	protected void okPressed() {
		String name = session.getItem(session.getSelectionIndex());
		log.info("selected session name=" + name);
		Session s = ServiceFactory.getSessionService().getByName(name);
		log.info("selected session=" + s);

		url = s.buildJdbcUrl();
		url.setUser(user.getText());
		url.setPassword(password.getText());
		super.okPressed();
	}

	@Override
	protected void cancelPressed() {
		url = null;
		super.cancelPressed();
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Session");
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.makeColumnsEqualWidth = false;
		layout.numColumns = 1;

		GridData d = new GridData(GridData.FILL_BOTH);
		d.verticalSpan = 1;
		d.horizontalSpan = 1;

		Composite con = new Composite(parent, SWT.NONE);
		con.setLayoutData(d);
		con.setLayout(layout);

		this.createSessionPane(con);
		this.createAuthGroup(con);

		String[] names = ServiceFactory.getSessionService().getAllNames().toArray(new String[0]);
		Arrays.sort(names);
		for (String n : names) {
			session.add(n);
		}

		String select = initSessionName;
		if (null == select) {
			select = names[0];
		}
		this.selectSession(select);

		return parent;
	}

	private Composite createSessionPane(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.makeColumnsEqualWidth = false;
		layout.numColumns = 3;

		GridData d = new GridData(GridData.FILL_HORIZONTAL);
		d.verticalSpan = 1;
		d.horizontalSpan = 1;

		Composite c = new Composite(parent, SWT.NONE);
		c.setLayoutData(d);
		c.setLayout(layout);
		{
			d = new GridData();
			d.verticalSpan = 1;
			d.horizontalSpan = 1;

			Label label = new Label(c, SWT.NONE);
			label.setLayoutData(d);
			label.setText("Session");

			d = new GridData(GridData.FILL_HORIZONTAL);
			d.verticalSpan = 1;
			d.horizontalSpan = 2;

			session = new Combo(c, SWT.READ_ONLY);
			session.setLayoutData(d);
		}

		return c;
	}

	private Group createAuthGroup(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.makeColumnsEqualWidth = false;
		layout.numColumns = 2;

		GridData d = new GridData(GridData.FILL_BOTH);
		d.verticalSpan = 1;
		d.horizontalSpan = 1;

		Group group = new Group(parent, SWT.NONE);
		group.setLayoutData(d);
		group.setLayout(layout);
		group.setText("Auth");

		{
			d = new GridData();
			d.verticalSpan = 1;
			d.horizontalSpan = 1;

			Label label = new Label(group, SWT.NONE);
			label.setLayoutData(d);
			label.setText("User");

			d = new GridData(GridData.FILL_HORIZONTAL);
			d.verticalSpan = 1;
			d.horizontalSpan = 1;

			user = new Text(group, SWT.BORDER);
			user.setLayoutData(d);
		}
		{
			d = new GridData();
			d.verticalSpan = 1;
			d.horizontalSpan = 1;

			Label label = new Label(group, SWT.NONE);
			label.setLayoutData(d);
			label.setText("Password");

			d = new GridData(GridData.FILL_HORIZONTAL);
			d.verticalSpan = 1;
			d.horizontalSpan = 1;

			password = new Text(group, SWT.BORDER | SWT.PASSWORD);
			password.setLayoutData(d);
		}

		return group;
	}
}
